<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="编号" prop="cno">
        <el-input v-model="queryParams.cno" placeholder="请输入编号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <!-- <el-form-item label="所属单位" prop="deptId">
        <el-input v-model="queryParams.deptId" placeholder="请输入所属单位" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="归属仓库" prop="storeId">
        <el-input v-model="queryParams.storeId" placeholder="请输入归属仓库" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="归属库区" prop="areaId">
        <el-input v-model="queryParams.areaId" placeholder="请输入归属库区" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="所在货柜" prop="groupId">
        <el-input v-model="queryParams.groupId" placeholder="请输入所在货柜" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['wms:cabinet:add']">新增</el-button>
        <el-button type="warning" plain icon="el-icon-plus" size="mini" @click="handleBatchAdd" v-hasPermi="['wms:cabinet:add']">批量新增</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['wms:cabinet:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wms:cabinet:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wms:cabinet:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wms:cabinet:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->


    <el-descriptions class="margin-top" :column="4" border>
        <el-descriptions-item>
          <template slot="label">
            编号
          </template>
          {{groupInfo.cno}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            名称
          </template>
          {{groupInfo.name}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在库区
          </template>
          {{groupInfo.areaName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在仓库
          </template>
          {{groupInfo.storeName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            架子数
          </template>
          {{groupInfo.cnums}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            格口数
          </template>
          {{groupInfo.lnums}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            物品数
          </template>
          {{groupInfo.totals}}
        </el-descriptions-item>


      </el-descriptions>
    <el-row v-loading="loading" class="mt10" :gutter="5">
      <el-col :span="4"  v-for="item in cabinetList" class="mb5">
        <el-card class="box-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span>{{item.name}}</span>
            <el-button class="fr" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(item)" v-hasPermi="['wms:cabinet:remove']"></el-button>
            <el-button class="fr mr5" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(item)" v-hasPermi="['wms:cabinet:edit']"></el-button>
          </div>
          <div>
            <el-link type="warning" @click="handleCols(item)">格口数:[{{item.lnums}}]</el-link>
            <el-link class="ml10 fr" type="primary" @click="handleProperty(item)">物品数:[{{item.totals}}]</el-link>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改柜号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="580px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="编号" prop="cno">
          <el-input v-model="form.cno" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
          <el-form-item label="排序" prop="seq">
            <el-input v-model="form.seq" placeholder="请输入排序" />
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="创建架子" :visible.sync="openBatch" width="360px" append-to-body>
      <el-form ref="batchForm" :model="batchForm" :rules="batchRules" label-width="110px">
        <el-form-item label="编号" prop="cno">
          <el-input v-model="batchForm.cno" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="起始编码" prop="startNo">
          <el-input v-model="batchForm.startNo" placeholder="请输入起始编码 例如:1" />
        </el-form-item>
        <el-form-item label="截止编码" prop="endNo">
          <el-input v-model="batchForm.endNo" placeholder="请输入起始编码 例如:99" />
        </el-form-item>
        <el-form-item label="层数" prop="floors">
          <el-input v-model="batchForm.floors" placeholder="请输入层数" />
        </el-form-item>
          <el-form-item label="起始排序" prop="seq">
            <el-input v-model="batchForm.seq" placeholder="请输入排序" />
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitBatchForm">确 定</el-button>
        <el-button @click="cancelBatch">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCabinet,
    getCabinet,
    delCabinet,
    addCabinet,
    updateCabinet,
    initBean,
    addBatchCabinet
  } from "@/api/wms/cabinet";
  import { treeselect} from "@/api/system/dept";
  import { storeTreeselect } from "@/api/wms/store";
  import { storeAreasTree } from "@/api/wms/storeArea";
  import { cabinetGroupTree,getCabinetGroup } from "@/api/wms/cabinetGroup";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "Cabinet",
    dicts:['cabinet_status'],
    components:{Treeselect},
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 柜号表格数据
        cabinetList: [],
        //单位数据
        deptOptions:[],
        //仓库数据
        storeOptions:[],
        //库位数据
        areaOptions:[],
        //智能柜数据
        cabinetGroupOptions:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openBatch:false,
        batchForm:{},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 50,
          id: undefined,
          name: undefined,
          cno: undefined,
          deptId: undefined,
          storeId: undefined,
          areaId: undefined,
          groupId: undefined,
          type: undefined,
          status: undefined,
          delFlag: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          cno: [{
            required: true,
            message: "编号不能为空",
            trigger: "blur"
          }],
        },
        batchRules: {
          startNo: [{
            required: true,
            message: "起始编码不能为空",
            trigger: "blur"
          }],
          endNo: [{
            required: true,
            message: "截止编码不能为空",
            trigger: "blur"
          }],
          name: [{
            required: true,
            message: "编码不能为空",
            trigger: "blur"
          }],
          floors: [{
            required: true,
            message: "层数不能为空",
            trigger: "blur"
          }],
        },
        cabinetGroupId:undefined,
        groupInfo:{}
      };
    },
    created() {
      const groupId = this.$route.query.groupId;
      this.queryParams.groupId = groupId;
      this.cabinetGroupId = groupId;
      this.getList();
      this.getTreeselect();
      this.getStoreTreeselect();
      if(groupId){
        this.getGroupInfo()
      }
    },
    watch:{

    },
    methods: {
      /** 查询柜号列表 */
      getList() {
        this.loading = true;
        listCabinet(this.queryParams).then(response => {
          this.cabinetList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getInitBean(){
        initBean({groupId:this.queryParams.groupId}).then(response=>{
            this.form = response.data;
        })
      },
      getGroupInfo(){
        getCabinetGroup(this.cabinetGroupId).then(response => {
          this.groupInfo = response.data;
        });
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      /** 查询仓库信息 */
      getStoreTreeselect() {
        storeTreeselect().then(response => {
          this.storeOptions = response.data;
        });
      },
       /** 查询库区信息 */
      getAreaTreeselect(storeId){
        storeAreasTree({storeId:storeId}).then(response=>{
          this.areaOptions = response.data;
        })
      },
       /** 查询智能柜信息 */
      getCabinetGrouoTreeselect(areaId){
        cabinetGroupTree({areaId:areaId}).then(response=>{
          this.cabinetGroupOptions = response.data;
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          cno: undefined,
          deptId: undefined,
          storeId: undefined,
          areaId: undefined,
          groupId: undefined,
          type: undefined,
          status: "0",
          pnums: undefined,
          lnums: undefined,
          seq: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancelBatch() {
        this.openBatch = false;
        this.resetBatch();
      },
      resetBatch() {
        this.batchForm = {
          startNo: undefined,
          endNo: undefined,
          name:undefined,
          type:undefined
        };
        this.resetForm("batchForm");
      },
      handleBatchAdd(){
        this.resetBatch();
        initBean({groupId:this.queryParams.groupId}).then(response=>{
            this.batchForm = response.data;
            this.batchForm.cabinetGroupId = this.cabinetGroupId;
            this.openBatch = true;
        })

      },
      submitBatchForm(){
        this.$refs["batchForm"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            addBatchCabinet(this.batchForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openBatch = false;
              if(this.cabinetGroupId){
                this.getGroupInfo();
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      handleCols(row){
        this.$router.push({
          path: '/wms/storeLocator/list',
          query: {
            pageNum: this.queryParams.pageNum,
            cabinetId: row.id
          }
        });
      },
      handleProperty(row){
        this.$router.push({
          path: '/wms/inventories/list',
          query: {
            pageNum: this.queryParams.pageNum,
            cabinetId: row.id,
            status:'0'
          }
        });
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.form.groupId = this.queryParams.groupId;
        this.open = true;
        this.title = "添加货架";
        this.getInitBean();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getCabinet(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改货架";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateCabinet(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addCabinet(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除柜号编号为"' + row.cno + '"的数据项？').then(() => {
          this.loading = true;
          return delCabinet(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('wms/cabinet/export', {
          ...this.queryParams
        }, `cabinet_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
